<?php
if(!defined('DEDEINC')) exit('Request Error!');

function plus_ask(&$atts,&$refObj,&$fields)
{
	global $dsql,$_vars,$question;

	$attlist = "titlelen=40,row=8,typeid=0,ishd=0,isanlen=30,likeq='no',sort=";
  FillAtts($atts,$attlist);
  FillFields($atts,$fields,$refObj);
	extract($atts, EXTR_OVERWRITE);

	$wheresql = ' 1 ';
	
	if($likeq == 'yes'){
		$wheresql .= $question['tid2'] != '0' ? " And ask.tid2=".$question['tid2'] : " And ask.tid=".$question['tid'];
	}

	if($sort=='') {
		$orderby = 'order by ask.id desc';
  }
	else if($sort=='digest')
	{
		$wheresql .= ' And ask.digest=1 or ask.status=2';
		$orderby = ' order by ask.id desc,ask.dateline desc';
	}
	else if($sort=='commend')
	{
		$wheresql .= ' And ask.digest=1';
		$orderby = ' order by ask.id desc,ask.dateline desc';
	}
	else if($sort=='ok')
	{
		$wheresql .= ' And ask.status=1 ';
		$orderby = ' order by ask.id desc,ask.solvetime desc,ask.dateline desc';
	}
	else if($sort=='expiredtime')
	{
		$wheresql .= ' And ask.status=0 ';
		$orderby = ' order by ask.id desc,ask.expiredtime asc, ask.dateline desc';
	}
	else if($sort=='reward')
	{
		$wheresql .= ' And ask.status=0 ';
		$orderby = ' order by ask.id desc,ask.reward desc';
	}
	else if($sort=='replies')
	{
		$wheresql .= ' ';
		$orderby = "  order by ask.id desc,ask.replies desc,ask.dateline desc ";
	}
	else
	{
		$wheresql .= ' And ask.status=0 ';
		$orderby = ' order by ask.id desc,ask.disorder desc, ask.dateline desc';
	}
	$query = $ishd==1?"select ask.*, ans.content answer from `#@__ask` ask left join `#@__askanswer` ans on ask.id = ans.askid where $wheresql $orderby limit $row":"select ask.* from `#@__ask` as ask where $wheresql $orderby limit $row";
	//if($likeq == 'yes')echo $query;
	$dsql->SetQuery($query);
	$dsql->Execute('an');
  $rearr = array();
  while($row = $dsql->GetArray('an'))
  {

    if($row['tid2'] != 0)
	  {
	    $row['typelink'] = $row['typedata'] = " <a href='browser.php?tid2={$row['tid2']}'>{$row['tid2name']}</a>\r\n";
		$row['typename'] = $row['tid2name'];
		$row['typeurl'] = "browser.php?tid2={$row['tid2']}";
	  }
	else{
    	$row['typelink'] = $row['typedata'] = " <a href='browser.php?tid={$row['tid']}'>{$row['tidname']}</a>\r\n";
		$row['typename'] = $row['tidname'];
		$row['typeurl'] = "browser.php?tid={$row['tid']}";
	}
	$row['fulltitle'] = $row['title'];
    $row['title'] = cn_substr($row['title'],$titlelen);
	$row['dateline'] = date('Y-m-d',$row['dateline']);
	$row['arcurl'] = "question.php?id={$row['id']}";
	$row['answer'] = cn_substr($row['answer'], $isanlen);
    $rearr[] = $row;
  }

	return $rearr;
}
?>